#include "../import/head.h"
#include "helper_protocol.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_package/head.h"
#include "../helper_heart/head.h"
#include "../helper_http/head.h"
#include "../helper_net/head.h"
#include "../helper_tcp/head.h"
#include "../helper_udp/head.h"
#include "protocol_heart.h"
#include "protocol_ssl.h"

namespace xos_sgsj
{

    helper_protocol::helper_protocol()
    {
    }

    helper_protocol::~helper_protocol()
    {
    }

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// thread callback function
	// 

	int helper_protocol::run_task( xos::i_msg * pMsg )
	{
		int ret = 0;

		session * pSession = (session *)pMsg->get_void( 0, 0 );
		xos::i_buf * pBuf = pMsg->get_buf( 0, 0 );

		ret = helper_proc( pSession, pBuf );

		if( pMsg )
		{
			pMsg->release();
			pMsg = 0;
		}

		return ret;
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// worker function
	// 

	int helper_protocol::helper_proc( session * pSession, xos::i_buf * pBuf )
	{
		int ret = 0;

		xos::xos_u8 uCmd = 0;

		if( 0 == ret )
		{
			ret = g_pSysData->m_os.m_pSerial->un_pack_int8( pBuf, &uCmd );
		}

		if( 0 == ret )
		{
			ret = protocol_disp( pSession, pBuf, uCmd );
		}

		{
			data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
			p->init();
			p->m_nMsg = MSG_SESSION_REF;
			p->m_pSession = pSession;
			p->m_nRef = -1;
			if( 0 != ret )
			{
				p->m_nRef -= 1;
			}
			g_pSysData->m_pImpl->notify( p );
			p = 0;
		}

		if( pBuf )
		{
			pBuf->release();
			pBuf = 0;
		}

		return ret;
	}

	int helper_protocol::protocol_disp( session * pSession, xos::i_buf * pBuf, int nCmd )
	{
		int ret = 0;

		switch( nCmd )
		{
		case PROTOCOL_HEART:
			{
				protocol_heart obj;
				ret = obj.dis_compose( pSession, pBuf );
			}
			break;
		case PROTOCOL_SSL:
			{
				protocol_ssl obj;
				ret = obj.dis_compose( pSession, pBuf );
			}
			break;
		default:
			{
				ret = 1;
			}
			break;
		}

		return ret;
	}

} // xos_sgsj
